import axios from 'axios';
import router from '@/router';

axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8';
const request = axios.create({ baseURL: '/api', timeout: 10000 });

request.interceptors.request.use(config => {
    return config;
});

request.interceptors.response.use(
    res => {
        if (res.status == 200) {
            return res;
        }
        console.error('寄');
        throw new Error('网络异常');
    },
    error => {
        let { message } = error;
        if (
            message == 'Network Error' ||
            message == 'Request failed with status code 500'
        ) {
            router.push({
                path: '/ErrorPage',
            });
        }
    }
);

export default request;
